<?php
/**
 * 轮播图模型
 */

namespace app\api\model;

use PDOStatement;
use think\Collection;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Model;

class Statistical extends Model {

    public function getStatistical($params) {
        date_default_timezone_set('PRC');
//        $m_time=strtotime(date('Y-m',strtotime('-1 month')));
//        $where="count_time={$m_time} and city_code={$params['city_code']}";
        $where="city_code={$params['city_code']}";
        $data = db('house_statistics_city_month')->where($where)->order("count_time desc")->find();
        //统计总览
        deal_money_list($data['deal_money'], $price, $price_unit);
        $count['deal_money'] = $price;//总成交金额
        $count['deal_money_unit'] = $price_unit;//单位

        $countData=array(
            ['name'=>'总成交量','value'=>$data['deal_total_number']?:0,'change'=>$data['deal_number_ratio']>0?1:0,'ratiao'=>abs($data['deal_number_ratio'])],
            ['name'=>'总成交率','value'=>$data['deal_rate']?:0,'change'=>$data['deal_rate_ratio']>0?1:0,'ratiao'=>abs($data['deal_rate_ratio'])],
            ['name'=>'成交金额','value'=>$price?:0,'unit'=>$price_unit?:'元','change'=>$data['deal_money_ratio']>0?1:0,'ratiao'=>abs($data['deal_money_ratio'])],
            ['name'=>'竞价人数','value'=>$data['bidders_number']?:0,'change'=>$data['bidders_number_ratio']>0?1:0,'ratiao'=>abs($data['bidders_number_ratio'])],

        );
        $info['countData']=$countData;
        $url_info=array(
            ['name'=>'详情数据','value'=>$data['info_url']?:'']
        );
        //详情数据
        $info['urlInfo']=$url_info;
        //成交价格比重
        $priceData=array(
                'total'=>$data['deal_total_number']?$data['deal_total_number']:0,
                'data'=>array(
                    ['value'=>$data['money_number_100']?:0,'name'=>'100万以下'],
                    ['value'=>$data['money_number_300']?:0,'name'=>'100-300万'],
                    ['value'=>$data['money_number_500']?:0,'name'=>'300-500万'],
                    ['value'=>$data['money_number']?:0,'name'=>'500万以上']
                )
        );
        $info['priceData']=$priceData;
        //成交面积比重
        $areaData=array(
                'total'=>$data['deal_total_number']?$data['deal_total_number']:0,
                'data'=>array(
                    ['value'=>$data['area_number_90']?:0,'name'=>'刚需90m²以下'],
                    ['value'=>$data['area_number_140']?:0,'name'=>'刚改90-140m²'],
                    ['value'=>$data['area_number_210']?:0,'name'=>'改善140-210m²'],
                    ['value'=>$data['area_number']?:0,'name'=>'豪宅210m²以上']
                )
        );
        $info['areaData']=$areaData;
        $info['date_time']=array('name'=>'月','value'=>date('m',$data['count_time']));
        return $info;
    }


    public function getAreaData($params){
        $one=$this->getTotalRate($params);//数量与成交率
        $two=$this->getDealPrice($params);//成交单价
        $three=$this->getDiscount($params);//折扣
        $four=$this->getNumber($params);//竞价人数
        $info['rate']=$one?$one:[];
        $info['price']=$two?$two:[];
        $info['discount']=$three?$three:[];
        $info['number']=$four?$four:[];
        return $info;
    }

    /**
     * 数量与成交率
     * @param $where
     * @return mixed
     */
    public function getTotalRate($params){
        $total_number='(select b.total_number from erp_house_statistics_area_month b where b.adcode=a.city_code order by b.count_time desc limit 1 ) total_number';
        $deal_total_number='(select b.deal_total_number from erp_house_statistics_area_month b where b.adcode=a.city_code order by b.count_time desc limit 1 ) deal_total_number';
        $deal_rate='(select b.deal_rate from erp_house_statistics_area_month b where b.adcode=a.city_code order by b.count_time desc limit 1 ) deal_rate';
        $data=  Db::query("select a.name city_name,{$total_number},{$deal_total_number},{$deal_rate} from erp_region  a where a.p_code={$params['city_code']}");
        foreach ($data as $key =>$value){
            $data[$key]['total_number']=$value['total_number']?:0;
            $data[$key]['deal_total_number']=$value['deal_total_number']?:0;
            $data[$key]['deal_rate']=$value['deal_rate']?:0;
        }
        $last_names = array_column($data,'deal_rate');
        array_multisort($last_names,SORT_DESC,$data);
        $total_number=array_column($data,'total_number');//总套数
        $deal_total_number=array_column($data,'deal_total_number');//成交总套数
        $deal_rate=array_column($data,'deal_rate');//成交率
        $title=array_column($data,'city_name');//区域
        $list[]=array(
            'name'=>'成交套数',
            'data'=>$deal_total_number,
        );
        $list[]=array(
            'name'=>'总套数',
            'data'=>$total_number,
        );
        $info['name']='数量与成交率';
        $info['data']=$list;
        $info['title']=$title;
        $info['rate']=$deal_rate;

        return $info;
    }

    /**
     * 成交单价
     * @param $where
     * @return mixed
     */
    public function getDealPrice($params){
        $deal_price='(select b.deal_price from erp_house_statistics_area_month b where b.adcode=a.city_code order by b.count_time desc limit 1 ) deal_price';

        $data=  Db::query("select a.name city_name,{$deal_price} from erp_region  a where a.p_code={$params['city_code']}");
        foreach ($data as $key =>$value){
            $data[$key]['deal_price']=$value['deal_price']?ceil($value['deal_price']/100):0;
        }
        $last_names = array_column($data,'deal_price');
        array_multisort($last_names,SORT_DESC,$data);
        $title=array_column($data,'city_name');//区域
        $pirce=array_column($data,'deal_price');//单价
        $info['name']='成交单价';
        $info['data'][]=array(
            'name'=>'成交单价',
            'data'=>$pirce
        );
        $info['title']=$title;
        return $info;
    }

    /**
     * 折扣
     * @param $where
     * @return mixed
     */
    public function getDiscount($params){
        $discount_ratio='(select b.discount_ratio from erp_house_statistics_area_month b where b.adcode=a.city_code order by b.count_time desc limit 1 ) discount_ratio';

        $data=  Db::query("select a.name city_name,{$discount_ratio} from erp_region  a where a.p_code={$params['city_code']}");
        foreach ($data as $key =>$value){
            $data[$key]['discount_ratio']=$value['discount_ratio']?:0;
        }
        $last_names = array_column($data,'discount_ratio');
        array_multisort($last_names,SORT_DESC,$data);
        $title=array_column($data,'city_name');//区域
        $discount_ratio=array_column($data,'discount_ratio');//折扣
        $info['name']='折扣比';
        $info['data'][]=array(
            'name'=>'折扣比',
            'data'=>$discount_ratio
        );
        $info['title']=$title;
        return $info;
    }
    /**
     * 竞价人数
     * @param $where
     * @return mixed
     */
    public function getNumber($params){
        $bidders_number='(select b.bidders_number from erp_house_statistics_area_month b where b.adcode=a.city_code order by b.count_time desc limit 1 ) bidders_number';

        $data=  Db::query("select a.name city_name,{$bidders_number} from erp_region  a where a.p_code={$params['city_code']}");
        foreach ($data as $key =>$value){
            $data[$key]['bidders_number']=$value['bidders_number']?:0;
        }
        $last_names = array_column($data,'bidders_number');
        array_multisort($last_names,SORT_DESC,$data);
        $title=array_column($data,'city_name');//区域
        $bidders_number=array_column($data,'bidders_number');//竞价人数
        $info['name']='竞价人数';
        $info['data'][]=array(
            'name'=>'竞价人数',
            'data'=>$bidders_number
        );
        $info['title']=$title;
        return $info;
    }



}
